package xyz.hubery.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@RestController
public class AuthorationController {

    @Autowired
    private PasswordEncoder passwordEncoder;
    /**
     * 获取当前登录的用户对象
     */
    @GetMapping("/user/info")
    public Principal getPrincipal(Principal principal) {
        // 等同于：
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return principal;
    }

    /**
     * 密码加密
     */
    @PatchMapping("/user/password/encode")
    public String updateLoginPassword(String password) {
        return passwordEncoder.encode(password);
    }

    /**
     * 密码校验
     */
    @GetMapping("/user/password/match")
    public boolean mathcPassword(String rawPassword,String encodedPassword) {
        return passwordEncoder.matches(rawPassword,encodedPassword);
    }

}
